/* Copyright (c) 2012 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.gdata.aeuploader;


import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.logging.LogManager;


/**
 * The Class FileUploadDemo.
 * 
 * A demonstration of uploading using local files.
 */
public class FileUploadDemo {

  /** The Constant uploadUrl. */
  private static final String uploadUrl = "https://docs.google.com/" +
  		"feeds/upload/create-session/default/private/full?convert=false";
    
  /**
   * Prompt the user for input.
   *
   * @param prompt the prompt
   * @return the string
   * @throws IOException Signals that an I/O exception has occurred.
   */
  private static String PromptUser(String prompt) throws IOException {
    System.out.print(prompt);
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
    return bufferedReader.readLine();
  }
  
  /**
   * Gets the auth header from the user.
   *
   * @return the string
   * @throws IOException Signals that an I/O exception has occurred.
   */
  private static String GetAuthHeader() throws IOException {
    String token = PromptUser("Enter your auth token: ");
    return "OAuth " + token;
  }
  
  /**
   * Gets the filename from the user.
   *
   * @return the string
   * @throws IOException Signals that an I/O exception has occurred.
   */
  private static String GetFilename() throws IOException {
    String filename = PromptUser("Enter the filename: ");
    File file = new File(filename);
    if (!file.exists()) {
      System.out.println("File does not exist, try again.");
      return GetFilename();
    }
    return filename;
  }
  
  /**
   * Initialize logging.
   * 
   * So we get FINE log messages to the console.
   *
   * @throws IOException Signals that an I/O exception has occurred.
   */
  private static void InitializeLogging() throws  IOException {
    String config = "handlers = java.util.logging.ConsoleHandler" + "\n" + 
        ".level = ALL" + "\n" + "java.util.logging.ConsoleHandler.level = ALL";
    InputStream ins = new ByteArrayInputStream(config.getBytes());
    LogManager.getLogManager().readConfiguration(ins);
  }
  
  /**
   * The main method.
   *
   * @param args the arguments
   * @throws IOException Signals that an I/O exception has occurred.
   */
  public static void main(String[] args) throws IOException {
    InitializeLogging();
    Uploader uploader = new Uploader(
        new URL(uploadUrl),
        GetAuthHeader(),
        new FileBlobProvider(GetFilename())
    );
    uploader.Create();
  }

}
